<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WeekCheckout;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\AdminUser;


class WeekCheckoutController extends BaseController
{

    public $title = '周结算';

    protected function grid1()
    {
    }

    protected function grid2()
    {
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WeekCheckout(['admin_user']), function (Grid $grid) {
            $grid = $this->gridButton($grid, true, true, false);
            $grid->setActionClass(Grid\Displayers\ContextMenuActions::class);


            //只导出 id, name和email 三列数据
            $titles = ['mt_login'        => '*Account (账号)', 'mt4' => '*ManagerName (Manager名称)',
                       'profit_checkout' => '*Withdraw (投投出额)', 'Withdrawal' => '*Withdraw (备注信息)'
            ];
            $grid->export($titles)->rows(function (array $rows) {
                foreach ($rows as $index => &$row) {
                    $row['mt_login']        = AdminUser::where('id', $row['user_id'])->first()->mt_login;
                    $row['mt4']             = 'MT4-真实';
                    $row['profit_checkout'] = getNumberFormat($row['profit'], 2);
                    $row['Withdrawal']      = 'Withdrawal';
                }
                return $rows;
            })->filename('周数据汇总');

            // 周1至6
            $t = getFilterDate(6);
            $grid->model()->whereBetween('created_at', [$t['start'], $t['end']]);
//                ->groupBy('user_id')->select([
//                'user_id',
//                \DB::raw('SUM(profit) as profit'),
//                \DB::raw('SUM(profit_checkout) as profit_checkout'),
//            ]);

            $grid->column('admin_user.name', '姓名');
            $grid->column('admin_user.username', '登陆帐户');
            $grid->column('admin_user.mt_login', '推广账户');
            $grid->column('profit')->sortable();
            $grid->column('profit_checkout');
            $grid->column('created_at');
//            $grid->column('created_at')->display(function() use ($t){
//                return $t['start'];
//            });
            //$grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 rightSide 布局
                $filter->panel();
                //$filter->expand();
                //$filter->equal('id');
                $filter->equal('admin_user.name', '姓名')->width(2);
                $filter->whereBetween('created_at', function ($q) {
                    $start = $this->input['start'] ?? null;
                    $end   = $this->input['end'] ?? null;

                    $q->whereBetween('created_at', [$start, $end]);
                }, '日期')->date(1)->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WeekCheckout(), function (Show $show) {
            $show->id;
            $show->user_id;
            $show->profit;
            $show->profit_checkout;
            $show->all_data;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WeekCheckout(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('profit');
            $form->text('profit_checkout');
            $form->text('all_data');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
